package dfsketches.gr3d;

import java.util.Hashtable;
import java.util.Random;

public class FluctuatingAnimation implements Animation  {

	{
		rnd = new Random();
	}
	
	private static Random rnd;

	public FluctuatingAnimation(Hashtable<Bone, FluctuatingMatrixPair> matrixPairs) {
		fluct = matrixPairs;
	}
	
	@Override
	public Matrix3D getTransformation(Bone bone, float time) {
		return getTransformation(bone, time, new Matrix3D());
	}

	@Override
	public Matrix3D getTransformation(Bone bone, float time, Matrix3D output) {
		FluctuatingMatrixPair flPair = fluct.get(bone);
		if (flPair == null) return output.makeUnite();
		
		Matrix3D med = flPair.med;
		Matrix3D dev = flPair.dev;
		output.m11 = med.m11 + dev.m11 * (rnd.nextFloat() * 2f - 1f);
		output.m12 = med.m12 + dev.m12 * (rnd.nextFloat() * 2f - 1f);
		output.m13 = med.m13 + dev.m13 * (rnd.nextFloat() * 2f - 1f);
		output.m14 = med.m14 + dev.m14 * (rnd.nextFloat() * 2f - 1f);
		output.m21 = med.m21 + dev.m21 * (rnd.nextFloat() * 2f - 1f);
		output.m22 = med.m22 + dev.m22 * (rnd.nextFloat() * 2f - 1f);
		output.m23 = med.m23 + dev.m23 * (rnd.nextFloat() * 2f - 1f);
		output.m24 = med.m24 + dev.m24 * (rnd.nextFloat() * 2f - 1f);
		output.m31 = med.m31 + dev.m31 * (rnd.nextFloat() * 2f - 1f);
		output.m32 = med.m32 + dev.m32 * (rnd.nextFloat() * 2f - 1f);
		output.m33 = med.m33 + dev.m33 * (rnd.nextFloat() * 2f - 1f);
		output.m34 = med.m34 + dev.m34 * (rnd.nextFloat() * 2f - 1f);
		return output;
	}

	@Override
	public float getDuration() {
		return Float.MAX_VALUE;
	}
	
	private Hashtable<Bone, FluctuatingMatrixPair> fluct;

}